package com.southwind.dao.impl;

import com.southwind.dao.SystemAdminDao;
import com.southwind.entity.SystemAdmin;
import com.southwind.util.JDBCUtil;

import java.sql.*;

public class SystemAdminDaoImpl implements SystemAdminDao {

    @Override
    public SystemAdmin findByUsername(String username) {
        Connection con = JDBCUtil.getConnection();
        PreparedStatement stmt = null;
        ResultSet res = null;
        String sql = "select * from system_admin where username = ?";
        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, username);
            res = stmt.executeQuery();
            if(res.next())
            {
                int id = res.getInt(1);
                String uname = res.getString(2);
                String password = res.getString(3);
                String name = res.getString(4);
                String phone = res.getString(5);
                return new SystemAdmin(id, uname, password, name, phone);
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.release(con,stmt,res);
        }

        return null;
    }
}
